﻿using System;
using UnityEngine;

public static class CombatUtility
{
    public static bool ApplyDamage(Character attacker, Character receiver, float damage)
    {
        return ApplyDamageAndImpact(attacker, receiver, damage, Vector3.zero, Vector3.zero, 0f);
    }

    public static bool ApplyDamageAndImpact(Character attacker, Character receiver, float damage, Vector3 hitPoint, Vector3 hitNormal, float impact)
    {
        if (receiver == null)
        {
            return false;
        }
        damage = Mathf.Round(damage);
        Life life = receiver.life;
        if ((life == null) || life.isDied)
        {
            return false;
        }
        Combat.Create(attacker, life, damage);
        if (impact > 0f)
        {
            ApplyImpact(receiver, (Vector3) (-hitNormal * impact));
        }
        return true;
    }

    public static bool ApplyExplosionDamage(Character attacker, float damage, Vector3 origin, float radius, LayerMask layerMask)
    {
        return ApplyExplosionDamageAndImpact(attacker, damage, origin, radius, layerMask, 0f);
    }

    public static bool ApplyExplosionDamageAndImpact(Character attacker, float damage, Vector3 origin, float radius, LayerMask layerMask, float impact)
    {
        bool flag = false;
        foreach (Collider collider in Physics.OverlapSphere(origin, radius, (int) layerMask))
        {
            Vector3 vector = collider.bounds.center - origin;
            float a = 1f - (vector.magnitude / radius);
            a = Mathf.Max(a, 0f);
            if (ApplyDamageAndImpact(attacker, collider.GetCharacter(), damage * a, origin, -vector, impact * a))
            {
                flag = true;
            }
        }
        return flag;
    }

    public static bool ApplyImpact(Character target, Vector3 impact)
    {
        if (target == null)
        {
            return false;
        }
        Move component = target.GetComponent<Move>();
        if (component == null)
        {
            return false;
        }
        component.ApplyImpact(impact);
        return true;
    }
}

